#!/bin/bash

if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
    DIRTY="-dirty"
fi

COMMIT="$(git rev-parse --short HEAD)"
COMMIT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
GIT_TAG="${DRONE_TAG:-$(git tag -l --contains HEAD | head -n 1)}"

if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then
    IMAGE_PUSH_TAG="${GIT_TAG}"
    VERSION="$GIT_TAG"
else
    IMAGE_PUSH_TAG="${COMMIT_BRANCH}-head"
    VERSION="${COMMIT}${DIRTY}"
fi

if [ -z "$ARCH" ]; then
    ARCH="$(go env GOHOSTARCH)"
fi

if [ -z "$OS" ]; then
    OS="$(go env GOHOSTOS)"
fi

SUFFIX="-${ARCH}"

HELM_TAG="${TAG:-${VERSION}}"
HELM_VERSION="${HELM_TAG/v/}"
TAG="${TAG:-${VERSION}${SUFFIX}}"
REPO="${REPO:-rancher}"

if echo "$TAG" | grep -q dirty; then
    TAG="dev"
    HELM_TAG="dev"
    HELM_VERSION="0.0.0-dev"
fi
